package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoGalleryWorks;
import com.muyu.danmo.domain.DanmoGalleryWorksPictures;
import com.muyu.danmo.mapper.DanmoGalleryWorksMapper;
import com.muyu.danmo.service.IDanmoGalleryWorksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 画廊作品Service业务层处理
 *
 * @author yangle
 * @date 2024-10-23
 */
@Service
public class DanmoGalleryWorksServiceImpl implements IDanmoGalleryWorksService
{
    @Autowired
    private DanmoGalleryWorksMapper danmoGalleryWorksMapper;

    /**
     * 查询画廊作品
     *
     * @param id 画廊作品主键
     * @return 画廊作品
     */
    @Override
    public DanmoGalleryWorks selectDanmoGalleryWorksById(Long id)
    {
        DanmoGalleryWorks danmoGalleryWorks = danmoGalleryWorksMapper.selectDanmoGalleryWorksById(id);
        danmoGalleryWorks.setViews(danmoGalleryWorks.getViews()+1);
        danmoGalleryWorksMapper.updateDanmoGalleryWorks(danmoGalleryWorks);
        return danmoGalleryWorks;
    }

    /**
     * 查询画廊作品列表
     *
     * @param danmoGalleryWorks 画廊作品
     * @return 画廊作品
     */
    @Override
    public List<DanmoGalleryWorks> selectDanmoGalleryWorksList(DanmoGalleryWorks danmoGalleryWorks)
    {
        return danmoGalleryWorksMapper.selectDanmoGalleryWorksList(danmoGalleryWorks);
    }

    /**
     * 新增画廊作品
     *
     * @param danmoGalleryWorks 画廊作品
     * @return 结果
     */
    @Transactional
    @Override
    public Result<String> insertDanmoGalleryWorks(DanmoGalleryWorks danmoGalleryWorks) {
        try {
            danmoGalleryWorks.setCreateTime(DateUtils.getNowDate());
            danmoGalleryWorksMapper.insertDanmoGalleryWorks(danmoGalleryWorks);

            Long id = danmoGalleryWorks.getId();
            danmoGalleryWorksPicturesSetId(id, danmoGalleryWorks.getDanmoGalleryWorksPicturesList());
            addDanmoGalleryWorksPictures(danmoGalleryWorks.getDanmoGalleryWorksPicturesList());
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error("添加失败: " + e.getMessage());
        }
    }

    private void addDanmoGalleryWorksPictures(List<DanmoGalleryWorksPictures> danmoGalleryWorksPicturesList) {
        danmoGalleryWorksMapper.insertDanmoGalleryWorksPicturesList(danmoGalleryWorksPicturesList);
    }

    private void danmoGalleryWorksPicturesSetId(Long id, List<DanmoGalleryWorksPictures> danmoGalleryWorksPicturesList) {
        for (DanmoGalleryWorksPictures danmoGalleryWorksPictures : danmoGalleryWorksPicturesList) {
            danmoGalleryWorksPictures.setWorksId(id);
        }
    }

    @Transactional
    public void endAuction(Long id) {
        DanmoGalleryWorks galleryWorks = danmoGalleryWorksMapper.selectDanmoGalleryWorksById(id);
        if (galleryWorks != null && galleryWorks.getEndAuction().before(DateUtils.getNowDate())) {
            // 实现拍卖结束逻辑，例如确定胜出者
            sendSmsNotification(galleryWorks);
        }
    }

    private void sendSmsNotification(DanmoGalleryWorks galleryWorks) {
        // 发送短信通知
        String message = "拍卖结束通知：" + galleryWorks.getTitle() + "拍卖已结束";
        System.out.println(message);
//        SmsUtils.sendSms(galleryWorks.getContactInfo(), message);
    }




    /**
     * 修改画廊作品
     *
     * @param danmoGalleryWorks 画廊作品
     * @return 结果
     */
    @Override
    public Result<String> updateDanmoGalleryWorks(DanmoGalleryWorks danmoGalleryWorks)
    {
        danmoGalleryWorks.setUpdateTime(DateUtils.getNowDate());
        danmoGalleryWorksMapper.updateDanmoGalleryWorks(danmoGalleryWorks);
        danmoGalleryWorksPicturesSetId(danmoGalleryWorks.getId(), danmoGalleryWorks.getDanmoGalleryWorksPicturesList());
        deleGalleryWorksPictures(danmoGalleryWorks.getId());
        addDanmoGalleryWorksPictures(danmoGalleryWorks.getDanmoGalleryWorksPicturesList());
        return Result.success("修改成功");
    }

    private void deleGalleryWorksPictures(Long id) {
        danmoGalleryWorksMapper.deleGalleryWorksPictures(id);
    }


    /**
     * 批量删除画廊作品
     *
     * @param ids 需要删除的画廊作品主键
     * @return 结果
     */
    @Override
    public int deleteDanmoGalleryWorksByIds(Long[] ids)
    {
        return danmoGalleryWorksMapper.deleteDanmoGalleryWorksByIds(ids);
    }

    /**
     * 删除画廊作品信息
     *
     * @param id 画廊作品主键
     * @return 结果
     */
    @Override
    public int deleteDanmoGalleryWorksById(Long id)
    {
        return danmoGalleryWorksMapper.deleteDanmoGalleryWorksById(id);
    }


}
